package com.gitee.melin.bee.util;

import lombok.extern.slf4j.Slf4j;

import java.time.DateTimeException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

@Slf4j
public class DateUtils {

    public static final DateTimeFormatter DEFAULT_DATE_FORMAT =
            DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault());

    public static final DateTimeFormatter DEFAULT_TIME_FORMAT =
            DateTimeFormatter.ofPattern("HH:mm:ss");

    public static final DateTimeFormatter DEFAULT_DATETIME_FORMAT =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());

    public static String formatDateTime(Instant dateTime) {
        if (dateTime == null) {
            return null;
        }

        return DEFAULT_DATETIME_FORMAT.format(dateTime);
    }

    public static String getCurrentDate() {
        return DEFAULT_DATE_FORMAT.format(Instant.now());
    }

    public static String getCurrentDateTime() {
        return DEFAULT_DATETIME_FORMAT.format(Instant.now());
    }

    public static String formatDateTime(Long timestamp) {
        Instant dateTime = Instant.ofEpochMilli(timestamp);
        return DEFAULT_DATETIME_FORMAT.format(dateTime);
    }

    public static String formatDateTime(Instant dateTime, String formate) {
        if (dateTime == null) {
            return null;
        }
        try {
            DateTimeFormatter format = DateTimeFormatter.ofPattern(formate)
                    .withZone(ZoneId.systemDefault());
            return format.format(dateTime);
        } catch (DateTimeException ex) {
            throw new RuntimeException(ex.getMessage(), ex);
        }
    }
}
